<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['diy']=$this->plugin_setting('diy');
if($_RQ['op']=='main'){
	exi($return);
}
/*******op*******/
if($_RQ['op']=='list'){
	//查询员工的订单
	$worker_id=pdo_getcolumn('jy_meifa_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');
	$params=array('wid'=>$worker_id,'uniacid'=>$_SESSION['uniacid']);
		if(isset($_RQ['status']) && $_RQ['status'] !=10){
		$params['status']=intval($_RQ['status']);
		$return['status']=$_RQ['status'];
	}else{
		$return['status']=10;
	}
	$order_list=pdo_getall('jy_meifa_order',$params,'','','createtime asc');
	if($order_list){
		foreach($order_list as &$item){
			$user=pdo_get('core_members',array('id'=>$item['user_id'],'uniacid'=>$_SESSION['uniacid']));
			$store=pdo_get('jy_meifa_store',array('id'=>$item['sid'],'uniacid'=>$_SESSION['uniacid']));
			$worker=pdo_get('jy_meifa_worker',array('id'=>$item['wid'],'uniacid'=>$_SESSION['uniacid']));
			$item['user_name']=$user['realname'];
			$item['user_nickname']=$user['nickname'];
			$item['store_title']=$store['title'];
			$basic=$this->plugin_setting('basic');
			$item['service']=$basic['service'];
			$item['store_image']=$store['image'];
			$item['worker']=$worker;
			$num=pdo_getcolumn('jy_meifa_num',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$item['id']),'number');
			$item['paidui']=pdo_count('jy_meifa_order',array('uniacid'=>$_SESSION['uniacid'],'wid'=>$item['wid'],'createtime <'=>$item['createtime'],'status'=>0));
			if($num<10){
				$item['number']=$worker['qz'].'000'.$num;
			}
			
			elseif($num>9 && $num<100){
				$item['number']=$worker['qz'].'00'.$num;
				}
			else{
				$item['number']=$worker['qz'].'0'.$num;
				}
			
		$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
			
		}
	}
	$return['order_list']=$order_list;
	exi($return);
}
//今日订单
if($_RQ['op']=='today'){
	$worker_id=pdo_getcolumn('jy_meifa_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');
	$params=array('wid'=>$worker_id,'uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['status']=$_RQ['status'];
	}
	//$params['day']=date("Y-m-d",time("day"));			
	$order_list=pdo_getall('jy_meifa_order',$params);
	// 当天的零点
	$dayBegin = strtotime(date('Y-m-d', time()));

	// 当天的24
	$dayEnd = $dayBegin + 24 * 60 * 60;
	if($order_list){
		foreach($order_list as $k=>$value){
			if($order_list[$k]['createtime'] <= $dayBegin || $order_list[$k]['createtime'] >= $dayEnd){
				unset($order_list[$k]);
			}
		}
			foreach($order_list as &$item){
			$user=pdo_get('core_members',array('id'=>$item['user_id'],'uniacid'=>$_SESSION['uniacid']));
			$store=pdo_get('jy_meifa_store',array('id'=>$item['sid'],'uniacid'=>$_SESSION['uniacid']));
			$worker=pdo_get('jy_meifa_worker',array('id'=>$item['wid'],'uniacid'=>$_SESSION['uniacid']));
			$item['user_name']=$user['realname'];
			$item['user_nickname']=$user['nickname'];
			$item['store_title']=$store['title'];
			$item['store_image']=$store['image'];
			$item['worker']=$worker;
			$num=pdo_getcolumn('jy_meifa_num',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$item['id']),'number');
		
			$item['paidui']=pdo_count('jy_meifa_order',array('uniacid'=>$_SESSION['uniacid'],'wid'=>$item['wid'],'createtime <'=>$item['createtime'],'status'=>0));
			
			
			if($num<10){
				$item['number']=$worker['qz'].'000'.$num;
			}
			
			elseif($num>9 && $num<100){
				$item['number']=$worker['qz'].'00'.$num;
				}
			else{
				$item['number']=$worker['qz'].'0'.$num;
				}
			
		$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
			
			}
			
		
	}
	$return['order_list']=$order_list;
	exi($return);
}
//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
	exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_meifa_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$order['package']=json_decode($order['package']);
	$order['createtime']=date('Y-m-d h:i',$order['createtime']);
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['user_id']));
	//项目信息
	$store=pdo_get('jy_meifa_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['sid']));	
	//print_r($serve);exit;
	$basic=$this->plugin_setting('basic');
	$return['service']=$basic['service'];
	$return['order']=$order;
	$return['member']=$member;
	$return['store']=$store;
	exi($return);
}

//员工确认订单
if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_meifa_order',array('status'=>'1'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$order=pdo_get('jy_meifa_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$store=pdo_get('jy_meifa_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['sid']));
	$worker=pdo_get('jy_meifa_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['wid']));
	$this->notice('notice',$_SESSION['uid'],$store['id'],$_RQ['id']);
	exi('开始'.$return['pluginset']['worktitle']);
}
//过号
if($_RQ['op']=='guo'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_meifa_order',array('status'=>'3'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$order=pdo_get('jy_meifa_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$store=pdo_get('jy_meifa_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['sid']));
	$worker=pdo_get('jy_meifa_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['wid']));
	$this->notice('cancel',$_SESSION['uid'],$store['id'],$_RQ['id']);
	exi('过号成功');
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('jy_meifa_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除预约成功');
}